
MSK:	DB	00
WY:	DB	00
EX:	DB	00
EKS:	DB	00
SSIZ	EQU	02000H
RSIZ	EQU	040H
SORG	EQU	0C000H


;	POINT MODE: MOVE CURSOR RIGHT

PXPL::	LXI	H,MSK
	MOV	A,M
	ANI	0AAH
	JZ	PXP1
	RRC
	MOV	M,A
	RET
PXP1:	MOV	A,M
	RRC
	RRC
	RRC
	MOV	M,A
	ANI	0A0H
	RZ
	LDA	EX
	ADI	004H
	STA	EX
	RET

;	POINT	MODE:	MOVE	CURSOR	LEFT

PXMI::	LXI	H,MSK
	MOV	A,M
	ANI	055H
	JZ	PXM1
	RLC
	MOV	M,A
	RET
PXM1:	MOV	A,M
	RLC
	RLC
	RLC
	MOV	M,A
	ANI	005H
	RZ
	LDA	EX
	SUI	004H
	STA	EX
	RET

;	POINT MODE: MOVE CURSOR UP

PYPL::	LXI	H,MSK
	MOV	A,M
	ANI	033H
	JZ	PYP1
	RLC
	RLC
	MOV	M,A
	RET
PYP1:	MOV	A,M
	RRC
	RRC
	MOV	M,A
	LDA	WY
	SUI	002H
	STA	WY
	RET

;	POINT MODE: MOVE CURSOR DOWN

PYMI::	LXI	H,MSK
	MOV	A,M
	ANI	0CCH
	JZ	PYM1
	RRC
	RRC
	MOV	M,A
	RET
PYM1:	MOV	A,M
	RLC
	RLC
	MOV	M,A
	LDA	WY
	ADI	002H
	STA	WY
	RET

;	LOAD ACC WITH MASK, HL WITH BYTE ADDRESS FROM
;	(MSK,WY,EX)

DOT:	LHLD	WY
	MOV	A,H
	MVI	H,SORG/02000H
	DAD	H
	DAD	H
	DAD	H
	DAD	H
	DAD	H
	RRC
	RRC
	ORA	L
	MOV	L,A
	LDA	MSK
	RET

;	POINT MODE: HOME CURSOR

PHOM::	LXI	H,08080H
	SHLD	WY
	MVI	A,040H
	STA	MSK
	RET

;	POINT MODE: MAKE WHITE DOT AT CURSOR

PWHIT::	CALL	DOT
	ORA	M
	MOV	M,A
	RET

;	POINT MODE: MAKE BLACK DOT AT CURSOR

PBLAK::	CALL	DOT
	CMA
	ANA	M
	MOV	M,A
	RET

;	POINT MODE: REVERSE DOT UNDER CURSOR

PREV::	CALL	DOT
	XRA	M
	MOV	M,A
	RET

;	POINT MODE: TEST DOT UNDER CURSOR

PTEST::	CALL	DOT
	ANA	M
	RET

;	BOTH MODES: CLEAR SCREEN TO WHITE

WHIT::	LXI	D,SSIZ
	LXI	H,SORG
WHI1:	MVI	M,0FFH
	INX	H
	DCR	E
	JNZ	WHI1
	DCR	D
	JNZ	WHI1
	RET

;	BOTH MODES: CLEAR SCREEN TO BLACK

BLAK::	LXI	D,SSIZ
	LXI	H,SORG
BLA1:	MVI	M,000H
	INX	H
	DCR	E
	JNZ	BLA1
	DCR	D
	JNZ	BLA1
	RET

;	 BOTH MODES: REVERSE ENTIRE SCREEN

REVE::	LXI	D,SSIZ
	LXI	H,SORG
REV1:	MOV	A,M
	XRI	0FFH
	MOV	M,A
	INX	H
	DCR	E
	JNZ	REV1
	DCR	D
	JNZ	REV1
	RET

;	POINT MODE: GIVEN A CARTESIAN ADDRESS, FORM A BIT
;	MASK AND BYTE ADDRESS FOR THE CURSOR

PCAR::	MOV	A,M
	ANI	0FCH
	STA	EX
	LDAX	D
	ANI	0FEH
	STA	WY
	MVI	B,80H
	MOV	A,M
	RRC
	JNC	PCA1
	MOV	C,A
	MOV	A,B
	RRC
	MOV	B,A
	MOV	A,C
PCA1:	RRC
	JNC	PCA2
	MOV	A,B
	RRC
	RRC
	RRC
	RRC
	MOV	B,A
PCA2:	LDAX	D
	RRC
	MOV	A,B
	JNC	PCA3
	RRC
	RRC
PCA3:	STA	MSK
	RET

;	POINT MODE: DRAW A LINE BETWEEN TWO POINTS
;	GIVEN THE INCREMENTS (B,C)=(DX,DY)

PLIN::	MOV	B,M
	XCHG
	MOV	C,M
	LXI	H,PXPL
	MOV	A,B
	ORA	A
	JP	PLI1
	CMA
	INR	A
	MOV	B,A
	LXI	H,PXMI
PLI1:	LXI	D,PYMI
	MOV	A,C
	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OV	C,A
	RET

;	GRAY MODE: INTRABYTE LEFT SHIFT OF ONE PIXEL

GEXMI::	MOV	A,M
	RLC
	RLC
	RLC
	RLC
	MOV	B,A
	ANI	0E0H
	ORA	C
	MOV	C,A
	MOV	A,B
	ANI	00FH
	MOV	B,C
	MOV	C,A
	RET

;	GRAY MODE: SHIFT WHOLE SCREEN 1 PIXEL RIGHT

GEKSPL::	LXI	H,SORG
	LXI	D,RSIZ-1
GEKSP1:	PUSH	H
	DAD	D
	CALL	GEXPL
	POP	H
GEKSP2:	CALL	GEXPL
	MOV	M,B
	INX	H
	MOV	A,L
	ANI	03FH
	JNZ	GEKSP2
	MOV	A,H
	CPI	HIGH(SORG+SSIZ)
	JNZ	GEKSP1
	RET

;	GRAY MODE: SHIFT ENTIRE SCREEN LEFT ONE PIXEL

GEKSMI::	LXI	H,SORG+SSIZ-1
	LXI	D,-RSIZ+1
GEKSM1:	PUSH	H
	DAD	D
	CALL	GEXMI
	POP	H
GEKSM2:	CALL	GEXMI
	MOV	M,B
	MOV	A,L
	ANI	03FH
	DCX	H
	JNZ	GEKSM2
	MOV	A,H
	CPI	HIGH(SORG)-1
	JNZ	GEKSM1
	RET

;	GRAY MODE: MOVE CURSOR RIGHT ONE PIXEL

GXPL::	LXI	H,MSK
	MOV	A,M
	ANI	0F0H
	JZ	GXP1
	MVI	M,00FH
	RET
GXP1:	MVI	M,0F0H
	LDA	EX
	ADI	004H
	STA	EX
	RET

;	GRAY MODE: SHIFT CURSOR LEFT ONE PIXEL

GXMI::	LXI	H,MSK
	MOV	A,M
	ANI	00FH
	JZ	GXM1
	MVI	M,0F0H
	RET
GXM1:	MVI	M,00FH
	LDA	EX
	SUI	004H
	STA	EX
	RET

;	GRAY MODE: MOVE CURSOR UP ONE PIXEL

GYPL::	LDA	WY
	SUI	002H
	STA	WY
	RET

;	GRAY MODE: MOVE CURSOR DOWN ONE PIXEL

GYMI::	LDA	WY
	ADI	002H
	STA	WY
	RET

;	GRAY MODE: HOME CURSOR

GHOM::	LXI	H,08080H
	SHLD	WY
	MVI	A,00FH
	STA	MSK
	RET

;	GRAY MODE: MAKE CURSOR PIXEL WHITE

GWHI::	CALL	DOT
	ORA	M
	MOV	M,A
	RET

;	GRAY MODE: MAKE CURSOR PIXEL BLACK

GBLA::	CALL	DOT
	CMA
	ANA	M
	MOV	M,A
	RET

;	GRAY MODE: REVERSE CURSOR PIXEL

GREV::	CALL	DOT
	XRA	M
	MOV	M,A
	RET

;	GRAY MODE: SENSE CURSOR PIXEL

GSEN::	CALL	DOT
	ANA	M
	RZ
	PUSH	PSW
	ANI	0F0H
	POP	PSW
	RZ
	RRC
	RRC
	RRC
	RRC
	RET

;	GRAY MODE: RAISE GRAY LEVEL OF CURSOR PIXEL

GZPL::	CALL	DOT
	MOV	B,A
	CMA
	ANA	M
	MOV	C,A
	MOV	A,B
	ANA	M
	ADI	011H
	ANA	B
	ORA	C
	MOV	M,A
	RET

;	GRAY MODE: LOWER PIXEL UNDER CURSOR ONE GRAY LEVEL

GZMI::	CALL	DOT
	MOV	B,A
	CMA
	ANA	M
	MOV	C,A
	MOV	A,B
	ANA	M
	ADI	0FFH
	ANA	B
	ORA	C
	MOV	M,A
	RET

;	GRAY MODE: CARTESIAN COORDINATES TO MASK, BYTE	ADDRESS

GCART::	LDAX	D
	ANI	03FH
	RLC
	STA	WY
	MOV	A,M
	ANI	03FH
	RRC
	PUSH	PSW
	RLC
	RLC
	RLC
	STA	EX
	POP	PSW
	MVI	A,00FH
	JC	GCAR1
	XRI	0FFH
GCAR1:	STA	MSK
	RET

;	BLOCK OF COMBINED CURSOR MOVEMENTS AND CURSOR
;	INVERSIONS IN THE POINT MODE - FOR DEMONSTRATION

PN:	CALL	PREV
	CALL	PYPL
	JMP	PREV

PS:	CALL	PREV
	CALL	PYMI
	JMP	PREV

PE:	CALL	PREV
	CALL	PXPL
	JMP	PREV

PW:	CALL	PREV
	CALL	PXMI
	JMP	PREV

;	PLACE THE INDIVIDUAL NEGATIVES OF THE BYTES B,C IN H,L

NEGL:	MOV	A,C
	CMA
	INR	A
	MOV	L,A
	MOV	A,B
	CMA
	INR	A
	MOV	H,A
	RET


; BLOCK OF COMBINED SCREEN-CURSOR SHIFTS

PEA:	CALL	PEKSPL
	JMP	PXPL

PWE:	CALL	PEKSMI
	JMP	PXMI

PNO:	CALL	PWYEPL
	JMP	PYPL

PSO:	CALL	PWYEMI
	JMP	PYMI

;	SIMILAR	BLOCK	FOR	GREY	MODE

GEA:	CALL	GEKSPL
	JMP	GXPL

GWE:	CALL	GEKSMI
	JMP	GXMI

GNO:	CALL	FYPL
	JMP	GYPL

GSO:	CALL	FYMI
	JMP	GYMI

;	COMBINED	CURSOR	MOVEMENT	AND	8-LEVEL	GRAY	SHIFT

GE:	CALL	GZEH
	CALL	GXPL
	JMP	GZEH

GW:	CALL	GZEH
	CALL	GXMI
	JMP	GZEH

GN:	CALL	GZEH
	CALL	GYPL
	JMP	GZEH

GS:	CALL	GZEH
	CALL	GYMI
	JMP	GZEH

;	MOVE WHOLE SCREEN THROUGH 8 GRAY POINTS

GZEEH:	LXI	D,SSIZ
	LXI	H,SORG
GZEE1:	MOV	B,M
	MOV	A,B
	ANI	0F0H
	MOV	C,A
	MOV	A,B
	ADI	008H
	ANI	00FH
	ORA	C
	ADI	080H
	MOV	M,A
	INX	H
	DCR	E
	JNZ	GZEE1
	DCR	D
	JNZ	GZEE1
	RET

;	LIGHTEN THE WHOLE SCREEN BY ONE GREY POINT

GZEEPL:	LXI	D,SSIZ
	LXI	H,SORG
GZEEP1:	MOV	B,M
	MOV	A,B
	ANI	0F0H
	MOV	C,A
	MOV	A,B
	ADI	001H
	ANI	00FH
	ORA	C
	ADI	010H
	MOV	M,A
	INX	H
	DCR	E
	JNZ	GZEEP1
	DCR	D
	JNZ	GZEEP1
	RET

;	DARKEN WHOLE SCREEN BY ONE GRAY POINT

GZEEMI:	LXI	D,SSIZ
	LXI	H,SORG
GZEEM1:	MOV	B,M
	MOV	A,B
	ANI	0F0H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